//
//  CartView.m
//  jingjing2.0
//
//  Created by apple on 16/5/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CartView.h"
#import "CartViewTableViewCell.h"
#import "CustomButton.h"
#import "JianXiViewController.h"

@implementation CartView
{
  
    NSMutableArray *_priceArr;
    NSMutableArray *_countArr;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor whiteColor];
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width,frame.size.height) style:UITableViewStylePlain];
        _tableView.rowHeight=50;
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [_tableView registerNib:[UINib nibWithNibName:@"CartViewTableViewCell" bundle:nil] forCellReuseIdentifier:@"reuse"];
        [self addSubview:_tableView];
        _priceArr=[NSMutableArray array];
        _countArr=[NSMutableArray array];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeFrame:) name:@"changeFrame" object:nil];
    }
    return self;
}
-(void)changeFrame:(NSNotification *)info
{
    CGFloat height=[info.object floatValue];
    _tableView.frame=CGRectMake(0, 0, self.frame.size.width,height);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
        headView.backgroundColor=[UIColor whiteColor];
        
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
        label.text=@"净净洗衣篮";
        label.font=[UIFont systemFontOfSize:15];
        label.textColor=BLUECOLOR;
        [headView addSubview:label];
        
        UIImageView *imageDelete=[[UIImageView alloc]initWithFrame:CGRectMake(headView.frame.size.width-120, 15, 20, 20)];
        imageDelete.image=[UIImage imageNamed:@"deleteCat.jpg"];
        [headView addSubview:imageDelete];
        
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(headView.frame.size.width-100-5, 10, 100, 30)];
        [btn setTitle:@"清空洗衣篮" forState:UIControlStateNormal];
        btn.titleLabel.font=[UIFont systemFontOfSize:15];
        [btn setTitleColor:BLUECOLOR forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clearCart) forControlEvents:UIControlEventTouchUpInside];
        [headView addSubview:btn];
        return headView;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 50;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartViewTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    CustomButton *button=self.dataSource[indexPath.row];
    cell.name.text=button.title;
    cell.price.text=[NSString stringWithFormat:@"¥%.1f",button.btn.price];
    cell.number.text=button.label.text;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.deletaBlock=^(NSInteger count,NSString *title,NSInteger number,BOOL isAdd)
    {
        //如果正在进行增加件数操作
        if (isAdd) {
            //使接收过来的总价格加一个对应商品的价格
            _sumPrice+=button.btn.price;
            //使接收过来的总件数加一个对应商品的价格
            _count++;
            //上限99件
            if (number>99) {
                return ;
            }
        }else
        {
            //如果正在进行减少件数操作
            //使接收过来的总价格减一个对应商品的价格
            _sumPrice-=button.btn.price;
            //使接收过来的总件数减一个对应商品的价格
            _count--;
            //数量小于等于0件时移除清单上相应的一行
            if (number<=0) {
                if (self.dataSource.count>0)
                {
                    NSIndexPath *index=[NSIndexPath indexPathForRow:count inSection:0];
                    [self.dataSource removeObjectAtIndex:count];
                    [_tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationMiddle];
                }
            }
           //当清单列表没有任何数据时让清单隐藏
            if(self.dataSource.count==0)
            {
                [_delegate cartViewDownWithHeight:self.dataSource.count*50];
                [_delegate reduce];
            }
        }
        //数量改变时发起通知
        NSDictionary *dic=[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%ld",(long)number] forKey:title];
       NSDictionary *priceDic=[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%.1f",_sumPrice] forKey:[NSString stringWithFormat:@"%d",_count]];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"RowChange" object:dic];
        //总数量和总价格发生改变发起通知
        if (_count>0) {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"PriceChange" object:priceDic];
        }
       
        
    };
    return cell;
}

-(void)clearCart
{
    [self.dataSource removeAllObjects];
    [_tableView reloadData];
    //表上的数量对应减少，购物车变灰色，计价变为0
    [_delegate cartViewDownWithHeight:self.dataSource.count*50];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ClearCart" object:nil];
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
